Refactor collection traits and return types#2351
Refactor collection traits and return types#2351sethrj wants to merge 18 commits intoceleritas-project:developfrom
Conversation
Test summary 5 980 files 9 697 suites 19m 48s ⏱️ Results for commit 5031ed8. ♻️ This comment has been updated with latest results. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #2351 +/- ##
===========================================
+ Coverage 87.25% 87.26% +0.01%
===========================================
Files 1377 1377
Lines 43850 43838 -12
Branches 13874 13848 -26
===========================================
- Hits 38263 38257 -6
+ Misses 4527 4522 -5
+ Partials 1060 1059 -1
🚀 New features to boost your workflow:
|
Add explicit .get() calls when accessing values returned by Collection subscript operators that now return LdgWrapper<T const> instead of T const&. This fixes compilation errors in boolean contexts, comparisons, and arithmetic operations where implicit conversion is not available. Changes include: - Boolean context checks in assertions (VolumeSurfaceView.hh) - Conditional expressions with wrapped IDs (GridReflectivityExecutor.hh) - Comparison operations in grid searches (NonuniformGrid.hh) - Arithmetic operations with wrapped offsets (VolumePathAccumulator.hh) - Test assertions comparing expected values with wrapped data Prompt: "I've made a code change so that `LdgWrapper<T const>` rather than `T const&` is being returned in a number of places. Build the entire code base, fixing implicit conversion/comparison by accessing the returned/dereferenced value with `.get()` explicitly." Assisted-by: GitHub Copilot
This reverts commit 38edd6e.
There was a problem hiding this comment.
Pull request overview
This PR refactors Collection trait/storage plumbing and adjusts call sites/tests to consistently return LDG-aware wrappers/spans from const collections, improving host/device consistency and preventing accidental loss of LDG acceleration.
Changes:
- Simplify
Collectioninternals by collapsing redundant trait/storage helpers and centralizing storage on a singleStorageT. - Introduce
AutoLdgSpanand switch const collection accessors/call sites to use.get()where LDG wrappers are now returned. - Update multiple unit tests and a few geometry/physics call sites to accommodate LDG wrapper return types.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/corecel/math/Algorithms.test.cu | Adjusts kernel math calls for LDG wrapper element access (.get() / casts). |
| test/corecel/data/Collection.test.cc | Updates expectations for LDG wrapper/span return types and validates data() pointer types. |
| test/celeritas/optical/Cherenkov.test.cc | Updates test expectations to extract values from LDG wrappers with .get(). |
| test/celeritas/em/WentzelVIMsc.test.cc | Updates test expectations to extract values from LDG wrappers with .get(). |
| src/orange/OrangeTrackView.hh | Updates volume instance ID access for LDG wrapper semantics and clarifies lambda return type. |
| src/geocel/VolumeSurfaceView.hh | Ensures validity checks use .get() for LDG wrapper results. |
| src/geocel/VolumePathAccumulator.hh | Uses .get() to extract numeric offsets from LDG wrappers before arithmetic. |
| src/corecel/math/Algorithms.hh | Refactors fastpow SFINAE to return-type form. |
| src/corecel/grid/NonuniformGrid.hh | Updates comparisons/search predicates to extract values from LDG wrappers via .get(). |
| src/corecel/data/DeviceVector.hh | Minor doc cleanup (\todo) and removes an unused include. |
| src/corecel/data/DeviceAllocation.hh | Removes unused include and trims outdated TODO commentary. |
| src/corecel/data/detail/FillInvalid.hh | Removes an unused include. |
| src/corecel/data/detail/CollectionImpl.hh | Major refactor of collection traits/storage/copy helpers and LDG span selection. |
| src/corecel/data/Collection.hh | Updates Collection API/type aliases, storage member, inlined accessors, and moves free make_ref helpers. |
| src/corecel/cont/LdgSpan.hh | Adds AutoLdgSpan and tweaks remove_ldg_wrapper signature to match wrapper-based span. |
| src/celeritas/track/SimData.hh | Removes unused include after refactor ripple. |
| src/celeritas/optical/surface/model/GridReflectivityExecutor.hh | Uses .get() for ID truthiness checks with LDG wrappers. |
| src/celeritas/global/CoreTrackView.hh | Simplifies dereference of ObserverPtr returned by data() (drops redundant .get()). |
| src/celeritas/em/params/UrbanMscParams.cc | Removes unused type alias after ID/type refactor. |
| src/celeritas/em/msc/UrbanMsc.hh | Updates ID access to use .get(); adjusts member constness/comment for kernel argument passing. |
| src/celeritas/em/msc/detail/UrbanMscHelper.hh | Extracts IDs/indices appropriately under LDG wrapper semantics and updates indexing logic. |
| src/celeritas/em/data/UrbanMscData.hh | Moves UrbanParMatId type alias to a shared location and updates dependent field types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Const cast is OK because the only time it's used is when this is called | ||
| // with Ownership::reference and the caller is doing T* -> const T* | ||
| auto* data = const_cast<T*>(src.data()); | ||
| auto size = src.size(); |
There was a problem hiding this comment.
copy_collection unconditionally const_casts the source pointer (auto* data = const_cast<T*>(src.data());). This removes compile-time const-safety and makes it possible to create an Ownership::reference (mutable Span) view into a const Ownership::value collection (e.g., from a Collection<..., Ownership::value> const&), which would allow mutating elements of a const container and is undefined behavior. Consider overloading copy_collection for mutable vs const sources (or only casting when the source is known to be mutable) so that assigning/building Ownership::reference from a const-owned collection fails to compile rather than silently casting away const.
esseivaju
left a comment
There was a problem hiding this comment.
Looks good, two minor comments
| template<class T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true> | ||
| inline CELER_FUNCTION T fastpow(T a, T b) | ||
| template<class T> | ||
| inline CELER_FUNCTION std::enable_if_t<std::is_floating_point<T>::value, T> |
There was a problem hiding this comment.
| inline CELER_FUNCTION std::enable_if_t<std::is_floating_point<T>::value, T> | |
| inline CELER_FUNCTION std::enable_if_t<std::is_floating_point_v<T>, T> |
| using SpanT = Span<type>; | ||
| using SpanConstT = Span<const_type>; | ||
| using const_type = T; //!< Return type is *mutable* for reference! | ||
| using SpanConstT = Span<T>; |
There was a problem hiding this comment.
-- Commented on the wrong line, should be line 70 -- Should we also redefine StorageT since SpanT is redefined? We wouldn't expect a user to have modifiable storage through Ownership::const_reference
| using SpanConstT = Span<T>; | |
| using SpanConstT = Span<T>; | |
| using StorageT = SpanT; |
A number of places that could have been using
__ldgare not, due to implicit casting to Span (see #2348) and inconsistent behavior between host and device. Now, instead of using LdgSpan for device and Span for host, this always returns LdgSpan when returning from const collections for consistency: syntax/conversion errors will behave the same for both cases. Due to the overly relaxed implicit span constructor, a number of LdgSpan are now being returned but converted to Span under the hood.This PR also removes some of the extra templating and redundant traits classes used in collection. It should slightly reduce the amount of object code due to simplified templates, will make debugging easier due to one nested class implementation, and generally should be easier to read.